package com.ychen.ruijee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ychen.ruijee.bean.AddressBook;
import com.ychen.ruijee.common.BaseContext;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.soap.AddressingFeature;
import java.awt.event.WindowFocusListener;
import java.util.List;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-07 15:57
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> saveAddressBook(@RequestBody AddressBook addressBook){
        boolean flag = addressBookService.saveAddressBook(addressBook);
        return flag ? R.success("保存收货地址成功"):R.error("保存收货地址失败");
    }


    /**
     * 根据 当前的登录 id 查询当前用户的 全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        List<AddressBook> result = addressBookService.listAddressBook(addressBook);
        return R.success(result);
    }

    /**
     * 修改收货地址 后数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AddressBook lookAddressBook(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null,AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getById(id);
        return addressBook;
    }

    // 修改收货地址信息
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        boolean flag = addressBookService.updateById(addressBook);
        return flag ? R.success("收货信息更新成功"):R.error("收货信息更新失败");
    }


    /**
     * 修改收货地址为 默认
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> toDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        // 先根据 id 更新 where userId = BaseContext.getCurrentId();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        log.info("【】更新后的对象是：{}",addressBook.toString());
        // 设置更新后的属性值  默认  0否 1是
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置默认地址成功");

    }

    @GetMapping("/default")
    public R<AddressBook> lookDefaultAddress(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        // 登录账号 默认字段
        wrapper.eq(AddressBook::getUserId,currentId);
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return addressBook == null ? R.error("查询默认地址失败") : R.success(addressBook);
    }



}
